<template>
  <el-dialog :title="subjectCont.id ? '编辑学科' : '新增学科'" :visible="dialogVisible" width="40%" :before-close="handleClose">
    <el-form :model="ruleForm" ref="ruleForm" :rules="rules" label-width="100px">
      <el-form-item label="学科名称" prop="subjectName">
        <el-input v-model="ruleForm.subjectName"></el-input>
      </el-form-item>
      <el-form-item label="是否显示">
        <el-switch active-color="#13ce66" inactive-color="#ff4949" v-model="ruleForm.isFrontDisplay"></el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    },
    subjectCont: {
      type: Object
    }
  },
  data () {
    return {
      ruleForm: {
        subjectName: '',
        isFrontDisplay: false
      },
      rules: {
        subjectName: [{ required: true, message: '请输入学科名称', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.ruleForm = this.subjectCont
    this.ruleForm.isFrontDisplay = this.subjectCont.isFrontDisplay === '是'
  },
  methods: {
    handleClose () {
      this.$refs.ruleForm.resetFields()
      this.$emit('close')
    },
    confirm () {
      this.$refs.ruleForm.validate(async (isOk) => {
        if (isOk) {
          if (this.subjectCont.id) {
            this.$emit('editSbj', this.ruleForm)
          } else {
            const data = { subjectName: this.ruleForm.subjectName, isFrontDisplay: this.ruleForm.isFrontDisplay ? 1 : 0 }
            this.$emit('addSubject', data)
            this.$message.success('学科添加成功')
          }
          this.handleClose()
        }
      })
    }
  }
}
</script>

<style scoped lang='less'></style>
